package com.czy.daishusys.ui.message;

import android.Manifest;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.support.design.widget.AppBarLayout;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.czy.daishusys.R;
import com.czy.daishusys.db.UserHelper;
import com.czy.daishusys.entity.StaffModel;
import com.czy.daishusys.entity.UserInfo;
import com.czy.daishusys.global.Constants;
import com.czy.daishusys.ui.BaseActivity;
import com.czy.daishusys.utils.GsonUtils;
import com.czy.daishusys.utils.IntentUtils;
import com.czy.daishusys.utils.MyLog;
import com.czy.daishusys.utils.PhoneUtils;
import com.czy.daishusys.utils.PicDialog;
import com.czy.daishusys.utils.TimeUtils;
import com.czy.daishusys.utils.glide.GlideUtils;
import com.czy.daishusys.widget.RightArrowButton;
import com.easemob.cases.MessageHelper;
import com.hyphenate.easeui.EaseConstant;
import com.hyphenate.easeui.domain.EaseUser;
import com.tbruyelle.rxpermissions2.RxPermissions;
import com.zhouyou.http.EasyHttp;
import com.zhouyou.http.callback.CallBack;
import com.zhouyou.http.exception.ApiException;
import com.zhouyou.http.model.HttpParams;

import butterknife.BindView;
import butterknife.OnClick;
import de.hdodenhof.circleimageview.CircleImageView;
import io.reactivex.annotations.NonNull;
import io.reactivex.functions.Consumer;

public class UserInfoActivity extends BaseActivity {

    @BindView(R.id.civ_head)
    CircleImageView civHead;
    @BindView(R.id.tv_user_name)
    TextView tvUserName;
    @BindView(R.id.tv_call_phone)
    TextView tvCallPhone;
    @BindView(R.id.tv_send_msg)
    TextView tvSendMsg;
    @BindView(R.id.rbtn_nick_name)
    RightArrowButton rbtnNickName;
    @BindView(R.id.rbtn_sex)
    RightArrowButton rbtnSex;
    @BindView(R.id.rbtn_phone)
    RightArrowButton rbtnPhone;
    @BindView(R.id.rbtn_address)
    RightArrowButton rbtnAddress;
    @BindView(R.id.rbtn_birthday)
    RightArrowButton rbtnBirthday;
    @BindView(R.id.rbtn_bond_store)
    RightArrowButton rbtnBondStore;
    @BindView(R.id.toolbar_title)
    TextView toolbarTitle;
    @BindView(R.id.toolbar_subtitle)
    TextView toolbarSubtitle;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.appbar)
    AppBarLayout appbar;
    private UserInfo userInfo;

    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_user_info;
    }

    @Override
    protected void initInjector() {
        if (PhoneUtils.isEmpty(getIntent().getStringExtra("userId"))) {
            PhoneUtils.ShowToastMessage(this, getResources().getString(R.string.finish_parameter_anomaly));
            finish();
        }
    }

    @Override
    protected void initViews() {
        getToolbarTitle().setText(R.string.user_info);
        postUserDetails();
    }

    @Override
    protected void updateViews() {

    }

    @OnClick({R.id.tv_call_phone, R.id.tv_send_msg})
    protected void OnViewClick(View view) {
        switch (view.getId()) {
            case R.id.tv_call_phone:
                if (userInfo != null)
                    IntentUtils.onClickCallPhone(UserInfoActivity.this, userInfo.getPhone());
                break;
            case R.id.tv_send_msg:
                if (userInfo == null || PhoneUtils.isEmpty(userInfo.getImKey())) {
                    return;
                }
                setResult(RESULT_OK);
//                finish();
                EaseUser easeUser = new EaseUser(userInfo.getImKey());
                easeUser.setNick(userInfo.getNickName());
                easeUser.setAvatar(userInfo.getUserLogo());
                MessageHelper.getInstance().saveContact(easeUser);
                Intent intent = new Intent(UserInfoActivity.this, ChatActivity.class);
                intent.putExtra(EaseConstant.EXTRA_USER_ID, userInfo.getImKey());
                startActivity(intent);
                break;
        }
    }

    public void setText() {


        if ("0".equals(userInfo.getSex())) {

            rbtnSex.setRightText(getResources().getString(R.string.sex_girl));
        } else {
            rbtnSex.setRightText(getResources().getString(R.string.sex_boy));
        }
        tvUserName.setText(userInfo.getNickName());
        rbtnPhone.setRightText(userInfo.getPhone());
        rbtnNickName.setRightText(userInfo.getNickName());

        rbtnAddress.setRightText(userInfo.getAddr());
        rbtnAddress.focusOnRightText();
        if (Long.parseLong(userInfo.getBirthday()) != 0) {
            rbtnBirthday.setRightText(TimeUtils.formatTimeDifference3(Long.parseLong(userInfo.getBirthday())));

        }
        rbtnBondStore.setRightText(PhoneUtils.getString(userInfo.getStoreName(), ""));
//        rbtnSkillArea;
        GlideUtils.loadImage(this, userInfo.getUserLogo(), civHead, new GlideUtils.ImageLoadListener<String, GlideDrawable>() {
            @Override
            public void onLoadingComplete(String uri, ImageView view, GlideDrawable resource) {

            }

            @Override
            public void onLoadingError(String source, Exception e) {

            }
        }, R.mipmap.ic_message_head_own, R.mipmap.ic_message_head_own);
    }

    //获取员工详情
    public void postUserDetails() {

        HttpParams params = new HttpParams();
        params.put("session", UserHelper.getInstance().getSession(this));
        params.put("userId", getIntent().getStringExtra("userId"));
        params.put("pid",UserHelper.getInstance().getDevicePid());
        EasyHttp.post(Constants.SYS_USER_INFO_OWNERINFO)
                .params(params)
                .timeStamp(true)//添加时间戳
                .execute(new CallBack<String>() {
                    @Override
                    public void onStart() {
                        mMProgressDialog.show(getResources().getString(R.string.loading_text));
                    }

                    @Override
                    public void onCompleted() {
                        mMProgressDialog.dismiss();
                    }

                    @Override
                    public void onSuccess(String result) {
                        MyLog.e("EasyHttp", result);
                        String data = PhoneUtils.checkResponseFlag(UserInfoActivity.this, result);
                        if (!PhoneUtils.isEmpty(data)) {
                            userInfo = GsonUtils.fromJson(data, UserInfo.class);
                            setText();
                        }
                    }

                    @Override
                    public void onError(ApiException e) {
                        mMProgressDialog.dismiss();
                    }
                });


    }
}
